package com.jspgou.cms.action;


import static com.jspgou.common.web.Constants.SPT;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.file.FileNameUtils;
import com.jspgou.common.upload.FileRepository;
import com.jspgou.common.web.ResponseUtils;
import com.jspgou.core.entity.Website;



/**
 * @author Tom
 */
public class CommonUpload {
	
	public static final String UPLOAD_PATH = "/u";
	public static final String PATH = "/u/";
	
	protected void swfPicsUpload(
			String root,
			Integer uploadNum,
			@RequestParam(value = "Filedata", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception {
		JSONObject data=new JSONObject();
		Website site = SiteUtils.getWeb(request);
			String origName = file.getOriginalFilename();
			String ext = FilenameUtils.getExtension(origName).toLowerCase(
					Locale.ENGLISH);
			String fileUrl;
			String dateDir = FileNameUtils.genPathName();
		    fileUrl = fileRepository.storeBySwf(UPLOAD_PATH, ext,file);
		    String relPath = PATH + dateDir + SPT + fileUrl;
			data.put("imgUrl", relPath);
			ResponseUtils.renderText(response, data.toString());
		
	}
	
	@Autowired
	protected FileRepository fileRepository;
	
	
	
}
